#####
#####
###     Creating Figure 4
#####
#####

library(foreign)
library(lattice)

###
###   Read value coordinates from file, 
###   "value coords, with names.txt"
###

val.coords <- read.table(file.choose(), header = T)

###
###   Read data from Stata dataset, "info.dta",
###   omit observations with missing values
###

info <- read.dta(file.choose())

###
###   Create new data frame from
###   data frame info, containing
###   variables for party ID graph
###

info2 <- na.omit(info[, c(1, 4, 5, 9)])

###
###   Create three-category party identification
###   variable, and calculate mean vectors for
###   respondents in the three categories. Also
###   calculate mean resultant lengths for the
###   category vectors.
###

info2$pty <- "Independent"

info2$pty[info2$postpid7 >= 1 & info2$postpid7 <= 3] <- "Democrat"

info2$pty[info2$postpid7 >= 5 & info2$postpid7 <= 7] <- "Republican"

mean.pty.d1 <- tapply(info2$dim1, info2$pty, mean, na.rm = T)

mean.pty.d2 <- tapply(info2$dim2, info2$pty, mean, na.rm = T)

pty.mean.lengths <- (mean.pty.d1^2 + mean.pty.d2^2)^.5

###
###   Plot mean vectors for party identification
###   categories, along with value points
###
###   THIS CREATES FIGURE 4A
###

xyplot(dim2 ~ dim1, data = info2,
       aspect = 1,
       panel = function (x, y) {
         panel.xyplot(val.coords$dim1, val.coords$dim2, pch = 4, 
                    cex = .5, col = "black")
         panel.text(val.coords$dim1, val.coords$dim2, 
                    labels = c("Freedom", "Equality", "Economic\nsecurity",  
                               "Morality", "Individualism", "Social\norder",       
                               "Patriotism"),
                    pos = 4, cex = .5)
         panel.segments(rep(0, 3), rep(0, 3), 
                        mean.pty.d1, mean.pty.d2, lwd = 1.5)
         panel.text(mean.pty.d1[c(1,2)], mean.pty.d2[c(1,2)], 
                    labels = names(mean.pty.d2)[c(1,2)], pos = 4)
         panel.text(mean.pty.d1[3], mean.pty.d2[3], 
                    labels = names(mean.pty.d2)[3], adj = c(.5, 1.2))
       },
       xlab = "",
       ylab = "",
       scales = list(draw = FALSE)
)

###
###   ANAVA on party ID
###

meand1 <- mean(info2$dim1)

meand2 <- mean(info2$dim2)

mean.res.length <- (meand1^2 + meand2^2)^.5

res.length <- length(info2$dim1) * mean.res.length

pty.between <- sum(pty.mean.lengths * as.vector(table(info2$pty))) - res.length

pty.within <- length(info2$dim1) - sum(pty.mean.lengths * as.vector(table(info2$pty)))

dfnum <- 2

dfdenom <- length(info2$dim1) - 3

pty.ms.between <- pty.between / dfnum

pty.ms.within <- pty.within / dfdenom

F.pty <- pty.ms.between / pty.ms.within

obs.prob.pty <- 1 - pf(F.pty, dfnum, dfdenom)


###
###   Create new data frame from
###   data frame info, containing
###   variables for ideology graph
###

info3 <- na.omit(info[, c(1,4,5,8)])

names(info3)

###
###   Create three-category ideology 
###   variable from the 0-100 scale
###   item from the CCES. Calculate
###   mean vectors for the three categories
###   and the mean resultant lengths.
###

info3$idl <- "Moderate"

info3$idl[info3$ideolpst < 41] <- "Liberal"

info3$idl[info3$ideolpst > 59] <- "Conservative"

mean.idl.d1 <- tapply(info3$dim1, info3$idl, mean, na.rm = T)

mean.idl.d2 <- tapply(info3$dim2, info3$idl, mean, na.rm = T)

idl.mean.lengths <- (mean.idl.d1^2 + mean.idl.d2^2)^.5

###
###   Plot mean vectors for ideology
###   categories, along with value points
###
###   THIS CREATES FIGURE 4B
###

xyplot(dim2 ~ dim1, data = info3,
       aspect = 1,
       panel = function (x, y) {
         panel.xyplot(val.coords$dim1, val.coords$dim2, pch = 4, 
                    cex = .5, col = "black")
         panel.text(val.coords$dim1[c(1,2,3,5,6,7)], 
                    val.coords$dim2[c(1,2,3,5,6,7)], 
                    labels = c("Freedom", "Equality", "Economic security",  
                               "Individualism", "Social\norder",       
                               "Patriotism"),
                    pos = 4, cex = .5)
         panel.text(val.coords$dim1[4], val.coords$dim2[4], 
                    labels = "Morality",
                    pos = 2, cex = .5)
         panel.segments(rep(0, 3), rep(0, 3), 
                        mean.idl.d1, mean.idl.d2, lwd = 1.5)
         panel.text(mean.idl.d1[2], mean.idl.d2[2], 
                    labels = names(mean.idl.d2)[2], pos = 4)
         panel.text(mean.idl.d1[c(1)], mean.idl.d2[c(1)], 
                    labels = names(mean.idl.d2)[c(1)],  adj = c(.5, 1.2))
         panel.text(mean.idl.d1[c(3)], mean.idl.d2[c(3)], 
                    labels = names(mean.idl.d2)[c(3)], adj = c(-.1,1.1))
       },
       xlab = "",
       ylab = "",
       scales = list(draw = FALSE)
)

###
###   ANAVA on ideology
###

meand1 <- mean(info3$dim1)

meand2 <- mean(info3$dim2)

mean.res.length <- (meand1^2 + meand2^2)^.5

res.length <- length(info3$dim1) * mean.res.length

idl.between <- sum(idl.mean.lengths * as.vector(table(info3$idl))) - res.length

idl.within <- length(info3$dim1) - sum(idl.mean.lengths * as.vector(table(info3$idl)))

dfnum <- 2

dfdenom <- length(info3$dim1) - 3

idl.ms.between <- idl.between / dfnum

idl.ms.within <- idl.within / dfdenom

F.idl <- idl.ms.between / idl.ms.within

obs.prob.idl <- 1 - pf(F.idl, dfnum, dfdenom)
